package nancal.mp.db.redis

import nancal.mp.db.mongo.mor
import nbcp.base.db.LoginUserModel
import nbcp.base.extend.*
import nbcp.base.json.*
import nbcp.base.utils.SpringUtil
import nbcp.myoql.db.mongo.query
import nbcp.myoql.db.redis.proxy.*
import org.slf4j.LoggerFactory
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import java.time.Duration

/**
 * Created by udi on 17-5-13.
 */

object rer {

    open class SystemGroup {
        val sshSync = RedisSetProxy("ssh-sync")

        fun token(token: String) =
            RedisStringProxy("token:${token}", Duration.ofHours(12).seconds.AsInt())

        fun imgValidateCode(token: String) = RedisStringProxy("imgValidateCode:${token}", 30)

        //            val imgValidateCode = RedisStringProxy("imgValidateCode", 180);
        //            val ipcity = RedisStringProxy("ipcity", 3);
        fun smsCode(key: String) =
            RedisStringProxy("smsCode:${key}", Duration.ofMinutes(5).seconds.AsInt());

        fun tempToken(key: String) =
            RedisStringProxy("tempToken:${key}", Duration.ofMinutes(15).seconds.AsInt());

        private fun wxOpenId(key: String) = RedisStringProxy("wxOpenId:${key}", 300)

        private val adminTokenValue by lazy {
            return@lazy SpringUtil.context.environment.getProperty("token");
        }


        fun getShopUserFromOpenId(openid: String): LoginUserModel? {
            var retString = wxOpenId(openid).get();
            if (retString.HasValue) {
                return retString.FromJson();
            }

            var ret: LoginUserModel? = null
            var user = mor.wx.wxAppUser.query()
                .where { it.wxUserInfo.openid mongoEquals openid }
                .toEntity()

            if (user == null && openid == adminTokenValue) {
                user = mor.wx.wxAppUser.query()
                    .where { it.loginName mongoEquals "admin" }
                    .toEntity()
            }

            if (user != null) {
                ret = LoginUserModel()
                ret.id = user.id;
                ret.name = user.name;
                ret.ext = user.wxUserInfo.openid;
                wxOpenId(openid).set(ret.ToJson())
            }
            return ret
        }


    }


    class IamGroup {
        fun apiToken(key: String) =
            RedisJsonProxy("apiToken:${key}", LoginUserModel::class.java, -1);

        /**
         * 用户的token
         */
        fun oauthToken(key: String) =
            RedisJsonProxy(
                "oauth2:access_token:${key}",
                LoginUserModel::class.java,
                Duration.ofDays(3).seconds.AsInt()
            );


        val apiOpenNumber = RedisNumberProxy("api-open");
        fun regexApi(key: Int) = RedisSortedSetProxy("regex-api:${key}");
        fun directApi(key: Int) = RedisSortedSetProxy("direct-api:${key}");
    }

    val sys by lazy { SystemGroup() }
    val iam by lazy { IamGroup() }


    fun <T> getString(key: String, type: Class<T>): T? {
        return RedisJsonProxy(key, type).get()
    }
}
